package com.fangs.test;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    public static Set<Integer> positionSet = new HashSet<>(); //用于存储选择的位置
    private boolean selectMode = false; //选择模式 多选或者单选 true 多选
    public Set<String> checkTYpeNameSet = new HashSet<>(); //用于存储选择项的名称
    private RecyclerView mRecyclerView, mrv, mRecycler;
    List<TestBean> list = new ArrayList<>();
    private MultipleRecyclerAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mRecyclerView = (RecyclerView) findViewById(R.id.rv);
        mrv = (RecyclerView) findViewById(R.id.rvs);
        mRecycler = (RecyclerView) findViewById(R.id.recycler);

        FlexboxLayoutManager manager = new FlexboxLayoutManager(this, FlexDirection.ROW, FlexWrap.WRAP) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        mRecyclerView.setLayoutManager(manager);

        for (int i = 0; i < 6; i++) {
            TestBean testBean = new TestBean();
            testBean.setName("张三——" + i);
            list.add(testBean);
        }

        mAdapter = new MultipleRecyclerAdapter(this, list);
        mRecyclerView.setAdapter(mAdapter);


        //多选
        /*mAdapter.setOnItemClickListener(new MultipleRecyclerAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                addOrRemove(position);
            }

            @Override
            public void OnItemLongClick(View view, int position) {
            }
        });*/


        //单选
        mAdapter.setOnItemClickListener(new MultipleRecyclerAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                if (selectMode) {
                    // 如果当前处于多选状态，则进入多选状态的逻辑
                    // 维护当前已选的position
                    addOrRemoves(position);
                } else {
                    // 如果不是多选状态，则进入单选事件的业务逻辑
                    if (!positionSet.contains(position)) {
                        // 选择不同的单位时取消之前选中的单位
                        positionSet.clear();
                    }
                    addOrRemoves(position);
                }
                String kindName = list.get(position).getName();
                Toast.makeText(MainActivity.this, kindName, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void OnItemLongClick(View view, int position) {
            }
        });


        List list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            list.add("哈萨ki" + i);
        }
        SeeMoreAdapter seeMoreAdapter = new SeeMoreAdapter(list);
        mrv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        mrv.setAdapter(seeMoreAdapter);
        seeMoreAdapter.setOnITEMClickListener(new SeeMoreAdapter.OnItemClick() {
            @Override
            public void onItemClick(int position) {
                int mPosition = position + 1;
                Toast.makeText(getBaseContext(), "点击了第" + mPosition + "个哈萨ki", Toast.LENGTH_SHORT).show();
            }
        });


        ArrayList<String> items = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            items.add("条目");
        }
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecycler.setAdapter(new ItemNumberAdapter(list));

    }

    /**
     * 多选
     *
     * @param position
     */
    private void addOrRemove(int position) {
        if (positionSet.contains(position)) {
            // 如果包含，则撤销选择
            positionSet.remove(position);
            checkTYpeNameSet.remove(list.get(position).getName());
        } else {
            // 如果不包含，则添加
            positionSet.add(position);
            checkTYpeNameSet.add(list.get(position).getName());
        }
        if (positionSet.size() == 0) {
            // 如果没有选中任何的item，则退出多选模式
            mAdapter.notifyDataSetChanged();
            selectMode = false;
        } else {
            // 更新列表界面，否则无法显示已选的item
            mAdapter.notifyDataSetChanged();
        }
        Log.e("info", positionSet.toString());
        Toast.makeText(this, checkTYpeNameSet.toString(), Toast.LENGTH_SHORT).show();
    }


    /**
     * 单选
     *
     * @param position
     */
    private void addOrRemoves(int position) {
        if (positionSet.contains(position)) {
            // 如果包含，则撤销选择
            positionSet.remove(position);
        } else {
            // 如果不包含，则添加
            positionSet.add(position);
        }
        if (positionSet.size() == 0) {
            // 如果没有选中任何的item，则退出多选模式
            mAdapter.notifyDataSetChanged();
            selectMode = false;
        } else {
            // 更新列表界面，否则无法显示已选的item
            mAdapter.notifyDataSetChanged();
        }
    }
}